// 汽车展示网站路由配置
export const routeConfig = [
  {
    path: '/',
    name: 'home',
    title: '首页',
    component: 'page',
    showInNav: true,
    icon: 'Home',
    order: 1,
  },
  {
    path: '/cars',
    name: 'cars',
    title: '全部车辆',
    component: 'cars/page',
    showInNav: true,
    icon: 'Car',
    order: 2,
    children: [
      {
        path: '/cars/new',
        name: 'newCars',
        title: '新车',
        component: 'cars/new/page',
        showInNav: true,
        parent: 'cars',
      },
      {
        path: '/cars/used',
        name: 'usedCars',
        title: '二手车',
        component: 'cars/used/page',
        showInNav: true,
        parent: 'cars',
      },
      {
        path: '/cars/electric',
        name: 'electricCars',
        title: '电动车',
        component: 'cars/electric/page',
        showInNav: true,
        parent: 'cars',
      },
      {
        path: '/cars/fuel',
        name: 'fuelCars',
        title: '燃油车',
        component: 'cars/fuel/page',
        showInNav: true,
        parent: 'cars',
      },
      {
        path: '/cars/[id]',
        name: 'carDetail',
        title: '车辆详情',
        component: 'cars/[id]/page',
        showInNav: false,
      }
    ]
  },
  {
    path: '/brands',
    name: 'brands',
    title: '汽车品牌',
    component: 'brands/page',
    showInNav: true,
    icon: 'Award',
    order: 3,
    children: [
      {
        path: '/brands/[slug]',
        name: 'brandDetail',
        title: '品牌详情',
        component: 'brands/[slug]/page',
        showInNav: false,
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    title: '关于我们',
    component: 'about/page',
    showInNav: true,
    icon: 'Info',
    order: 4,
  },
  {
    path: '/contact',
    name: 'contact',
    title: '联系我们',
    component: 'contact/page',
    showInNav: true,
    icon: 'Phone',
    order: 5,
  },
  {
    path: '/login',
    name: 'login',
    title: '登录',
    component: 'login/page',
    showInNav: false,
    icon: 'LogIn',
  },
  {
    path: '/admin',
    name: 'admin',
    title: '管理后台',
    component: 'admin/page',
    showInNav: false,
    requireAuth: true,
    icon: 'Settings',
    children: [
      {
        path: '/admin/cars',
        name: 'adminCars',
        title: '车辆管理',
        component: 'admin/cars/page',
        showInNav: true,
        requireAuth: true,
        parent: 'admin',
      },
      {
        path: '/admin/cars/add',
        name: 'adminCarsAdd',
        title: '添加车辆',
        component: 'admin/cars/add/page',
        showInNav: false,
        requireAuth: true,
      },
      {
        path: '/admin/cars/[id]/edit',
        name: 'adminCarsEdit',
        title: '编辑车辆',
        component: 'admin/cars/[id]/edit/page',
        showInNav: false,
        requireAuth: true,
      },
      {
        path: '/admin/brands',
        name: 'adminBrands',
        title: '品牌管理',
        component: 'admin/brands/page',
        showInNav: true,
        requireAuth: true,
        parent: 'admin',
      },
      {
        path: '/admin/settings',
        name: 'adminSettings',
        title: '系统设置',
        component: 'admin/settings/page',
        showInNav: true,
        requireAuth: true,
        parent: 'admin',
      }
    ]
  }
] as const;

// 生成路由路径对象
export const routes = routeConfig.reduce((acc, route) => {
  acc[route.name] = route.path;
  if ('children' in route && route.children) {
    route.children.forEach(child => {
      acc[child.name] = child.path;
    });
  }
  return acc;
}, {} as Record<string, string>);

// 获取主导航菜单
export const getMainNavigation = () => {
  return routeConfig
    .filter(route => route.showInNav && !('requireAuth' in route ? route.requireAuth : false))
    .sort((a, b) => ('order' in a ? a.order || 999 : 999) - ('order' in b ? b.order || 999 : 999));
};

// 获取管理后台导航
export const getAdminNavigation = () => {
  const adminRoute = routeConfig.find(route => route.name === 'admin');
  return adminRoute?.children?.filter(child => child.showInNav) || [];
};

// 获取车辆分类导航
export const getCarsNavigation = () => {
  const carsRoute = routeConfig.find(route => route.name === 'cars');
  return carsRoute?.children?.filter(child => child.showInNav) || [];
};

// 获取需要认证的路由
export const getProtectedRoutes = () => {
  const protectedRoutes: string[] = [];
  
  routeConfig.forEach(route => {
    if ('requireAuth' in route && route.requireAuth) {
      protectedRoutes.push(route.path);
    }
    if ('children' in route && route.children) {
      route.children.forEach(child => {
        if ('requireAuth' in child && child.requireAuth) {
          protectedRoutes.push(child.path);
        }
      });
    }
  });
  
  return protectedRoutes;
};